/*
 * Copyright (c) 2011 Junichiro Okuno
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Junichiro Okuno.
 */

package tcp_net;

import java.util.List;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
	Provides the text input and output static methods.
	@author J. Okuno
*/
public class TextInputOutput
{
		/**
			Output a string variable from the text file
			@param fileName a text file name (Path)
			@return text returns the String characters.
		*/
		public static String getText(String fileName)
		{
			String str = ""; String text = "";
			
			try
			{
				BufferedReader is = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
				while((str = is.readLine()) != null) 
				{ 
					if(!text.equals("")) { text += "\n" + str; }
					else { text += str; }
				}
				is.close();
			}
			catch(IOException ex) { text = "IO Error"; }
			
			return text;
		}
		
		/**
			Output List<String> from the text file
			@param fileName a text file name (Path)
			@return al_texts returns the Vector String lists as lines of characters.
		*/
		public static List<String> getTextAsList(String fileName)
		{
			String str = ""; 
			
			List<String> al_texts = new ArrayList();
			
			try
			{
				BufferedReader is = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
				while((str = is.readLine()) != null) { al_texts.add(str); }
				is.close();
			}
			catch(IOException ex) { System.out.println("IO Error"); }
			
			return al_texts;
		}
		
		/**
			Save a string variable to the text file
			@param fileName a text file name (Path)
			@param str string characters
			@return 0 if successful otherwise -1
		*/
		public static int setText(String fileName, String str)
		{
			try 
			{
				PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
				out.write(str);
				out.close();
				return 0;
			}
			catch(IOException ex) { return -1; }
		}
	
		/**
			Save a list of string texts to the text file
			@param fileName a text file name (Path)
			@param al_texts a list of string characters
			@return 0 if successful otherwise -1
		*/
		public static int setText(String fileName, List<String> al_texts)
		{
			try 
			{
				PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
				
				if(al_texts.size()>0)
				{
					for(String str : al_texts) { out.write(str+"\n"); }
				}
				
				out.close();
				return 0;
			}
			catch(IOException ex) { return -1; }
		}
}